<?

//-----------------------------------------------------------------------------------
function getPage($url, $referer, $timeout, $header){
    if(!isset($timeout))
        $timeout=30;
    $curl = curl_init();
    if(strstr($referer,"://")){
        curl_setopt ($curl, CURLOPT_REFERER, $referer);
    }
    curl_setopt ($curl, CURLOPT_URL, $url);
    curl_setopt ($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
    curl_setopt ($curl, CURLOPT_HEADER, (int)$header);
    curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
    $html = curl_exec ($curl);
    curl_close ($curl);
    return $html;
}
//-----------------------------------------------------------------------------------


//-----------------------------------------------------------------------------------
function postPage($url,$pvars,$referer,$timeout){
    if(!isset($timeout))
        $timeout=30;
    $curl = curl_init();
    $post = http_build_query($pvars);
    if(isset($referer)){
        curl_setopt ($curl, CURLOPT_REFERER, $referer);
    }
    curl_setopt ($curl, CURLOPT_URL, $url);
    curl_setopt ($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
    curl_setopt ($curl, CURLOPT_HEADER, 0);
    curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt ($curl, CURLOPT_POST, 1);
    curl_setopt ($curl, CURLOPT_POSTFIELDS, $post);
    curl_setopt ($curl, CURLOPT_HTTPHEADER,
        array("Content-type: application/x-www-form-urlencoded;charset=windows-1255"));
    $html = curl_exec ($curl);
    curl_close ($curl);
    return $html;
}
//-----------------------------------------------------------------------------------



function changeDate($inputDate) {
  $split = explode("-", $inputDate);
  $outputDate = "" . $split[2] . "." . $split[1] . "." . $split[0];
  Return $outputDate;
}


function changeTimestamp($inputDate) {
//echo $inputDate;
	$outputdate=explode(" ",$inputDate);
	$outputdate[0]=changeDate($outputdate[0]);
	return ($outputdate);
}


function gotopage($goto)
{
	echo "<script type=\"text/javascript\">setTimeout('window.location.href=\"{$goto}\"',0);</script>";
	die();
}


function refreshpage()
{

	echo "<script type=\"text/javascript\">
	setTimeout('window.location.href=\"".$_SERVER['REQUEST_URI']."\"',0);</script>";
	die();
}

class database_connection
	{
	public 	static $conn;
	private	static $result;

	function __construct( $conn_array )
	{
			self::$conn = mysql_connect($conn_array["host"], $conn_array["username"], $conn_array["password"]);
			
			// set charset
			mysql_query("SET NAMES 'utf8'", 	self::$conn) 
				or error::trigger("Could not set NAMES.");
					
			// select database
			mysql_select_db($conn_array["database"], 	self::$conn) 
				or error::trigger("Could not find database <b>".$conn_array["database"]."</b>, Verify the name again.");
	}

	public static function query( $sql )
	{
		self::$result = mysql_query( $sql,	self::$conn )
			 or error::trigger("Query Failed ! ".mysql_error());
			 
		return self::$result;
	}

	public static function fetch_object( $query_results )
	{
		return mysql_fetch_object( $query_results );
	}
	
	public static function fetch_array( $query_results )
	{
		return mysql_fetch_array( $query_results );
	}

	public static function num_rows( $query_results )
	{
		return mysql_num_rows( $query_results );
	}

	public static function affected_rows()
	{
		return mysql_affected_rows( self::$conn );
	}

	public static function clean( $string )
	{
		$string		= strip_tags($string);
		if (ini_get('magic_quotes_gpc')) 
		{
	    	$string = stripslashes( $string );
	    }
	    
		return  mysql_real_escape_string( $string );
	}
	
	function print_clean( $string )
	{
		return  stripslashes( $string );
	}
	
	function print_fast_query( $fields, $table, $conditions )
	{
		$query 	= self::query("SELECT {$fields} FROM {$table} {$conditions}");
		$row 	= self::fetch_object( $query );

		foreach ( $row as $key_field=>$the_field ) 
		{
			echo $the_field;
			if (count($row) > 1) echo "<br />";
		}
	}
		
	function run_fast_query( $fields, $table, $conditions )
	{
		$query 	= self::query("SELECT {$fields} FROM {$table} {$conditions}");
		$row 	= self::fetch_object( $query );
		return $row;
	}
		
	function run_count_query( $table, $conditions )
	{
		$query 	= self::query("SELECT count(*) as 'COUNT' FROM {$table} {$conditions}");
		$row 	= self::fetch_object( $query );
		return $row;
	}
	
} //class end
	
	



//עיצוב
class layout{
		
	function open_big_title($title)
		{
			echo "<div style=\"background :url('lib/images/regtitlesbg.gif')
			 no-repeat top center; margin-top:4px;font-weight:bold;margin-bottom:5px;
			width:505px; height:36px;color:white;font-size:22px;text-align:right;\" >
			<div style=\"padding-right:10px;\">"
			.$title."</div></div>";
		}
		

	}

//Error Reporting Class
class error
{
	private static $debugLevel;	
	
	#initialize error handling
    function __construct()
    {
	  		self::$debugLevel = ERROR_REPORTING;
	  		
	  		// Error reporting level
	  		error_reporting (E_ALL);
	  		
	        // Take over from PHP error & exception handling
	        set_error_handler(array($this, 'error_handler')); 
	        set_exception_handler(array($this, 'error_handler'));
	        
    } //--__construct


	#prints Error
    function error_handler($error_level, $error_message, $file, $line)
    {		
			    switch ($error_level) {
			        case E_NOTICE:
			        case E_USER_NOTICE:
			            $error_type = 'Notice';
			            break;
			
			        case E_WARNING:
			        case E_USER_WARNING:
			            $error_type = 'Warning';
			            break;
			
			        case E_ERROR:
			        case E_USER_ERROR:
			            $error_type = 'Fatal Error';
			            $EXIT = TRUE;
			            break;
			            
			        default:
			            $error_type = 'Unknown';
			            $EXIT = TRUE;
			            break;
			    }
			
			
				if (self::$debugLevel) {
        			echo "<p style='".ERROR_CSS."'>";
        			echo "<b>$error_type: </b>$error_message<br/>";
					echo "<b>File: </b>$file<br/>";
					echo "<b>Line: </b>$line<br />";
         			echo "</p>";
          			exit; //stop program
        		} else {
      				echo "<p style='".ERROR_CSS."'>".ERROR_REPORTING_OFF_MSG."</p>";
        			exit; //stop program
        		}

    } //-- errorHandler() end.
    
    #manualy print error
    public static function trigger ( $error_string )
    {
    		trigger_error( $error_string );
    } //-- trigger() end.



	#deinitialize
	public static function deinitialize()
	{
	 		self::$debugLevel = null;
	 		
	 		restore_error_handler();
	 		restore_exception_handler();
	} //-- DeInitialize() end.

}
	
?>